(function() {
	module('NQueens Tests');

	test('Setup OK', function() {
		ok(true);
	});

	test('test placing queen', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		var placed = controller.onPlace(1, 1);

		equal(placed, true);
		equal(testView.lastRefreshData[1 * 4 + 1] != 0, true);

	});

	test('test placing queen on not allowed position', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		controller.onPlace(1, 1);
		var dataAfterPlace = testView.lastRefreshData;

		var placed = controller.onPlace(2, 2);

		equal(placed, false);
		deepEqual(testView.lastRefreshData, dataAfterPlace);
	});

	test('test winning match', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		var placed;
		placed = controller.onPlace(1, 0);
		ok(placed);

		// Not allowed move
		placed = controller.onPlace(1, 2);
		ok(!placed);

		placed = controller.onPlace(3, 1);
		ok(placed);

		placed = controller.onPlace(0, 2);
		ok(placed);

		placed = controller.onPlace(2, 3);
		ok(placed);

		ok(testView.onSolvedCalled);
	});

	test('test stale match', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		var placed;
		placed = controller.onPlace(0, 0);
		ok(placed);

		placed = controller.onPlace(2, 1);
		ok(placed);

		placed = controller.onPlace(1, 3);
		ok(placed);

		ok(testView.onStaleCalled);
	});

	test('test start game', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		controller.show();
		var expected = [];
		for ( var i = 0; i < 4 * 4; i++) {
			expected.push(0);
		}

		deepEqual(testView.lastRefreshData, expected);
	});

	test('test onReset', function() {
		var testView = new TestView();
		var controller = new NQueensController(4, testView);

		controller.show();
		var initialData = testView.lastRefreshData.slice(0);

		controller.onPlace(0, 0);
		controller.onReset();

		deepEqual(testView.lastRefreshData, initialData);
	});

	function TestView() {
		this.onSolvedCalled = false;
		this.onStaleCalled = false;
	}

	TestView.prototype.init = function(controller) {
	};

	TestView.prototype.refresh = function(data) {
		this.lastRefreshData = data;
	};

	TestView.prototype.onSolved = function() {
		this.onSolvedCalled = true;
	};

	TestView.prototype.onStale = function() {
		this.onStaleCalled = true;
	};

}());